package xgenerator.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XmlUtils {

	/**
	 * XPathFactory
	 */
	private static XPathFactory factory = XPathFactory.newInstance();
	
	/**
	 * XPath
	 */
	private static XPath xpath = factory.newXPath();
	
	/**
	 * <p>
	 * Title：evaluateObject
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param query
	 * @param contextNode
	 * @param returnType
	 * @return
	 */
	public static Object evaluateObject(String query, Object contextNode, QName returnType) {
		Object result = null;
		
		try {
			XPathExpression expr = xpath.compile(query);
			result = expr.evaluate(contextNode, returnType);
		} catch (XPathExpressionException e) {
			throw new RuntimeException(e);
		}
		
		return result;
	}
	
	/**
	 * <p>
	 * Title：evaluateString
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param query
	 * @param contextNode
	 * @return
	 */
	public static String evaluateString(String query, Object contextNode) {
		String result = null;
		
		try {
			XPathExpression expr = xpath.compile(query);
			result = expr.evaluate(contextNode);
		} catch (XPathExpressionException e) {
			throw new RuntimeException(e);
		}
		
		return result;
	}
	
	/**
	 * <p>
	 * Title：getDocument(String xmlFile)
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param xmlFile
	 * @return
	 */
	public static Document getDocument(String xmlFile) {
		Document doc = null;
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setNamespaceAware(true); // never forget this!
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse(xmlFile);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return doc;
	}
	
	/**
	 * <p>
	 * Title：getDocument(InputStream is)
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param is
	 * @return
	 */
	public static Document getDocument(File xmlFile) {
		Document doc = null;

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setNamespaceAware(true); // never forget this!
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse(new FileInputStream(xmlFile));
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return doc;
	}
	
	/**
	 * <p>
	 * Title：getDocument(InputStream is)
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param is
	 * @return
	 */
	public static Document getDocument(InputStream is) {
		Document doc = null;

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setNamespaceAware(true); // never forget this!
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse(is);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != is) {
				try {
					is.close();
				} catch (Exception ignore) {
				}
			}
		}

		return doc;
	}
	
	public static void main(String[] args) {
		String DATA_SOURCES_CONFIG_FILE = System.getProperty("user.dir") + File.separator + "config" + File.separator + "DataSources.xml";
		Document doc = XmlUtils.getDocument(DATA_SOURCES_CONFIG_FILE);
		Object result = evaluateObject("//DataSource[@name=\"EAP_DS\"]/driverClassName/text()", doc, XPathConstants.NODESET);
		System.out.println(result);
		
		NodeList nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getNodeValue()); 
        }
	}
}
